home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / wais / waisgate / list.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  4KB  |  219 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.    
  4.    Brewster@think.com
  5. */
  6.  
  7. /* Change log:
  8.  * $Log:    list.c,v $
  9.  * Revision 1.4  92/03/07  19:44:00  jonathan
  10.  * changed function argument to sort_list from long to int.
  11.  * mycroft@hal.gnu.ai.mit.edu.
  12.  * 
  13.  * Revision 1.3  92/02/13  12:40:11  jonathan
  14.  * Corrected syntax for function as arguments.
  15.  * 
  16.  * Revision 1.2  92/02/12  13:33:36  jonathan
  17.  * Added "$Log" so RCS will put the log message in the header
  18.  * 
  19. */
  20.  
  21. /* List Utilities.
  22.  *
  23.  * -brewster
  24.  */
  25.  
  26.  
  27. #include "list.h"
  28.  
  29. /* a list is an end_of_list terminated array */
  30.  
  31. char empty_tmp = 'f';
  32. void *end_of_list = (void*)&empty_tmp;  /* this is special */
  33.  
  34. void* car(list)
  35. void **list;
  36. {
  37.   if(end_of_list == *list)
  38.     return(end_of_list);
  39.   else
  40.     return(*list);
  41. }
  42.  
  43. /* returns the nth element (0 is the first element).
  44.   it returns NULL if it asks for an element off the end (?).
  45.  */
  46.  
  47. void* nth(number, list)
  48. long number;
  49. void **list;
  50. {
  51.   if(number < length(list))
  52.     return(list[number]);
  53.   else
  54.     return(NULL); 
  55. }
  56.  
  57. void 
  58. setf_nth(number, elem, list)
  59. long number;
  60. void* elem;
  61. void**list;
  62. /* set the nth element */
  63. {
  64.   if(number < length(list))
  65.     list[number] = elem;
  66. }
  67.  
  68. void* first(list)
  69. void **list;
  70. {
  71.   return(car(list));
  72. }
  73.  
  74. void* second(list)
  75. void **list;
  76. {
  77.   return(car(cdr(list)));
  78. }
  79.  
  80. void* last(list)
  81. void **list;
  82. {
  83.   long len = length(list);
  84.   if (len > 0)
  85.     return(nth(len - 1,list));
  86.   else
  87.     return(NULL);
  88. }
  89.  
  90. void** cdr(list)
  91. void **list;{
  92.   if(NULL == list)
  93.     return((void**)end_of_list);
  94.   else if(end_of_list == *list)
  95.     return((void**)end_of_list);
  96.   else
  97.     return(list+1);
  98. }
  99.  
  100. void** nth_cdr(list, n)
  101. void **list;
  102. long n;{
  103.   void** l = list;
  104.   long i;
  105.   for (i = 0; i < n; i++)
  106.     l = cdr(l);
  107.   return(l);
  108. }
  109.  
  110. void** rest(list)
  111. void **list;
  112. {
  113.   return(cdr(list));
  114. }
  115.  
  116. void* cadr(list)
  117. void **list;
  118. {
  119.   if(NULL == list)
  120.     return(NULL);
  121.   else if(end_of_list == *list)
  122.     return(NULL);
  123.   else
  124.     return(*(list+1));
  125. }
  126.  
  127. /* length of a list.  returns -1 if error.*/
  128. long length(list)
  129. void** list;
  130. {
  131.   long count = 0;
  132.   if(list == NULL)
  133.     return(0);
  134.   while(end_of_list != list[count])
  135.     count++;
  136.   return(count);
  137. }
  138.  
  139. void mapcar(list, function)
  140. void **list;
  141. void (*function)();
  142. {
  143.   if(!null(list)){
  144.     (*function)(car(list));
  145.     mapcar(cdr(list), function);
  146.   }
  147. }
  148.  
  149. /* pushes the item on the end of the list. returns the list. */
  150. void **collecting(list, item)
  151. void **list;
  152. void *item;
  153. {
  154.   long len = length(list);
  155.   if(0 == len){
  156.     list = (void**)malloc(2 * sizeof(void*));
  157.     list[0] = item;
  158.     list[1] = end_of_list;
  159.     return(list);
  160.   }
  161.   else{
  162.     list = (void **)realloc((char *)list, (len + 2) * sizeof(void*));
  163.     list[len] = item;
  164.     list[len + 1] = end_of_list;
  165.   }
  166.   return(list);
  167. }
  168.  
  169.  
  170. void setf_car(list, item)
  171. void** list;
  172. void* item;
  173. {
  174.   list[0] = item;
  175. }
  176.  
  177. /* returns true if the list is NULL */
  178. boolean null(list)
  179. void **list;
  180. {
  181.   if(list == NULL)
  182.     return(true);
  183.   if(list[0] == end_of_list)
  184.     return(true);
  185.   return(false);
  186. }
  187.  
  188. boolean free_list(list)
  189. void **list;
  190. {
  191.   if(list != NULL)
  192.     s_free(list);
  193.   return(true);
  194. }
  195.  
  196. void
  197. sort_list(list, cmp)
  198. void** list;
  199. int (*cmp)();
  200.   qsort(list,length(list),sizeof(void*),cmp);
  201. }
  202.  
  203. void**
  204. remove_item_from_list(list, pos)
  205. void** list;
  206. long pos;
  207. {
  208.   long count = pos;
  209.   if (list == NULL || list[0] == end_of_list)
  210.     return(NULL);
  211.   while (end_of_list != list[count])
  212.    { list[count] = list[count + 1];
  213.      count++;
  214.    }
  215.   list = (void**)realloc((char*)list,count*sizeof(void*));
  216.   return(list);
  217. }
  218.